/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ruoyi.yz.domain.excel;

import static com.ruoyi.common.utils.DateUtil.format;
import com.ruoyi.yz.customs.order.CEB311Message;
import com.ruoyi.yz.customs.order.Order;
import com.ruoyi.yz.customs.order.OrderHead;
import com.ruoyi.yz.customs.order.OrderList;
import com.ruoyi.yz.domain.YouzanOrder;
import com.ruoyi.yz.wuliu.Receiver;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static java.util.Objects.nonNull;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.trimToNull;

/**
 *
 * @author wmao
 */
public final class XlsOrderHelper {

    public static XlsOrder transform(YouzanOrder order) {
        XlsOrder xlsOrder = new XlsOrder();
        if (nonNull(order)) {
            xlsOrder.setOrderNo(order.getOrderNo());
            CEB311Message message = (CEB311Message) order.getBody();
            if (nonNull(message) && nonNull(message.getOrder())) {
                Order msgOrder = message.getOrder();
                OrderHead head = msgOrder.getOrderHead();
                Receiver recv = head.getReceiver();
                if (isNotBlank(head.getBuyerName())) {
                    xlsOrder.setCustomName(head.getBuyerName());
                    xlsOrder.setCustomMobile(head.getBuyerTelephone());
                } else if (nonNull(recv) && isNotBlank(recv.getName())) {
                    xlsOrder.setCustomName(recv.getName());
                    StringBuilder cm = new StringBuilder();
                    if (isNotBlank(recv.getMobile())) {
                        cm.append(recv.getMobile());
                        if (isNotBlank(recv.getTel())) {
                            cm.append("/").append(recv.getTel());
                        }
                    } else if (isNotBlank(recv.getTel())) {
                        cm.append(recv.getTel());
                    }
                    xlsOrder.setCustomMobile(cm.toString());
                }
                if (isNotBlank(head.getConsigneeAddress())) {
                    xlsOrder.setCustomAddr(head.getConsigneeAddress());
                } else if (nonNull(recv) && isNotBlank(recv.getAddress())) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(recv.getProvince()).append(recv.getCity()).append(recv.getArea()).append(recv.getAddress());
                    xlsOrder.setCustomAddr(sb.toString());
                }

                xlsOrder.setDealTime(format(head.getOrderTime(), "yyyy-MM-dd HH:mm:ss"));
                StringBuilder comment = new StringBuilder(head.getNote());
                List<OrderList> msgItems = msgOrder.getOrderList();
                if (isNotEmpty(msgItems)) {
                    List<XlsOrderItem> xlsItems = new ArrayList<>();
                    msgItems.forEach((msgItem) -> {
                        if (nonNull(msgItem)) {
                            XlsOrderItem item = new XlsOrderItem();
                            item.setCode(msgItem.getItemNo());
                            item.setBarCode(msgItem.getBarCode());
                            item.setNumber(msgItem.getQty());
                            item.setOrderNo(order.getOrderNo());
                            item.setTotalPrice(msgItem.getTotalPrice());
                            xlsItems.add(item);
                            String itmNote = trimToNull(msgItem.getNote());
                            if (isNotBlank(itmNote) && !equalsIgnoreCase(itmNote, "null")) {
                                comment.append("/").append(itmNote);
                            }
                        }
                    });
                    xlsOrder.setItems(xlsItems);
                }
                xlsOrder.setComment(comment.toString());
            }
        }
        return xlsOrder;
    }

    public static List<String> getListOfTemplateSheetNames() {
        return Arrays.asList("订单", "货品");
    }

}
